package com.sqllog.admin.sql.service;

import com.sqllog.admin.framework.exception.base.BusinessException;
import com.sqllog.admin.sql.entity.CanalClientConfigInfo;
import com.sqllog.admin.sql.listener.DataBaseLogListener;
import com.sqllog.admin.sql.mapper.CanalClientConfigInfoMapper;
import com.sqllog.core.CanalClient;
import com.sqllog.listener.SimpleCanalEventListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 客户端配置
 *
 * @author chennengcheng
 * @date 2022-01-20 16:44
 * @remark
 */
@Service
public class CanalClientConfigInfoService {

    @Resource
    private CanalClientConfigInfoMapper canalClientConfigInfoMapper;

    public boolean updateListenerInstance(CanalClientConfigInfo canalClientConfigInfo){
        CanalClientConfigInfo clientConfigInfo = canalClientConfigInfoMapper.selectByHostAndName(canalClientConfigInfo.getHost(), canalClientConfigInfo.getInstanceName());
        if (Objects.nonNull(clientConfigInfo) && !clientConfigInfo.getId().equals(canalClientConfigInfo.getId())) {
            throw new BusinessException("不允许同一 host 下配置相同的实例名");
        }

        canalClientConfigInfo.setUpdateDate(new Date());
        return canalClientConfigInfoMapper.updateById(canalClientConfigInfo);
    }

    public boolean startClientListenerInstance(String id){
        CanalClientConfigInfo canalClientConfigInfo = canalClientConfigInfoMapper.selectById(id);
        if ("1".equals(canalClientConfigInfo.getStatus())) {
            throw new BusinessException("已经启动");
        }

        SimpleCanalEventListener simpleCanalEventListener = new DataBaseLogListener();
        List<SimpleCanalEventListener> simpleCanalEventListeners = new ArrayList<>();
        simpleCanalEventListeners.add(simpleCanalEventListener);
        CanalClient canalClient = CanalClientConfigInfo.startCanalClient(canalClientConfigInfo, simpleCanalEventListeners);
        boolean running = canalClient.isRunning();
        if (!running) {
            canalClientConfigInfoMapper.updateStatusById("3", canalClientConfigInfo.getId());
            throw new BusinessException("启动异常");
        }

        return true;
    }

    public boolean stopClientListenerInstance(String id){
        CanalClientConfigInfo canalClientConfigInfo = canalClientConfigInfoMapper.selectById(id);
        if (!"1".equals(canalClientConfigInfo.getStatus())) {
            throw new BusinessException("已经停止");
        }

        CanalClientConfigInfo.stopCanalClient(canalClientConfigInfo);
        return true;
    }

    public boolean addListenerInstance(CanalClientConfigInfo canalClientConfigInfo) {
        canalClientConfigInfo.setCreateDate(new Date());
        canalClientConfigInfo.setUpdateDate(new Date());
        CanalClientConfigInfo clientConfigInfo = canalClientConfigInfoMapper.selectByHostAndName(canalClientConfigInfo.getHost(), canalClientConfigInfo.getInstanceName());
        if (Objects.nonNull(clientConfigInfo)) {
            throw new BusinessException("不允许同一 host 下配置相同的实例名");
        }

        return canalClientConfigInfoMapper.insert(canalClientConfigInfo);
    }

    public List<CanalClientConfigInfo> listCanalClientConfigByName(String name) {
        return canalClientConfigInfoMapper.selectByInstanceName(name);
    }

    public CanalClientConfigInfo selectById(String id) {
        return canalClientConfigInfoMapper.selectById(id);
    }

    public List<CanalClientConfigInfo> selectByInstanceName(String instanceName) {
        return canalClientConfigInfoMapper.selectByInstanceName(instanceName);
    }

    public CanalClientConfigInfo selectByHostAndInstanceName(String host, String instanceName) {
        return canalClientConfigInfoMapper.selectByHostAndInstanceName(host, instanceName);
    }

    public boolean deleteById(String id) {
        return canalClientConfigInfoMapper.deleteById(id);
    }
}
